<template>
  <div class="search">
    <div class="search-box-wrapper">
      <search-box ref="searchBox" @query="onQueryChange" />
    </div>
    <div ref="shortcutWrapper" class="shortcut-wrapper" v-show="!query">
      <scroll :refreshDelay="refreshDelay" class="shortcut" ref="shortcut" :data="shortcut">
        <div>
          <div class="hot-key">
            <h1 class="title">热门搜索</h1>
            <ul>
              <li @click="addQuery(item.first)" class="item" v-for="item in hotKey" :key="item.first">
                <span>{{ item.first }}</span>
              </li>
            </ul>
          </div>
          <div class="search-history" v-show="searchHistory.length">
            <h1 class="title">
              <span class="text">搜索历史</span>
              <span class="clear" @click="showConfirm">
                <i class="iconfont icon-clear"></i>
              </span>
            </h1>
            <search-list @select="addQuery" @delete="deleteOne" :searches="searchHistory" />
          </div>
        </div>
      </scroll>
    </div>
    <div ref="searchResult" class="search-result" v-show="query">
      <suggest ref="suggest" @select="saveSearch" @listScroll="blurInput" :query="query" />
    </div>
    <confirm ref="confirm" text="是否清空所有搜索历史" confirmBtnText="清空" @confirm="deleteAll" />
    <router-view></router-view>
  </div>
</template>

<script>
import SearchBox from '../../base/search-box/SearchBox'
import Suggest from '../suggest/Suggest'
import SearchList from '../../base/search-list/SearchList'
import Confirm from '../../base/confirm/Confirm'
import Scroll from '../../base/scroll/Scroll'
import { getHotKey } from '../../api/index'
import { mapActions } from 'vuex'
import { playlistMixin, searchMixin } from '../../assets/js/mixin'

export default {
  name: 'Search',
  mixins: [playlistMixin, searchMixin],
  data() {
    return {
      hotKey: []
      // query: ''
    }
  },
  computed: {
    shortcut() {
      // 因为scroll有两块元素是异步获取数据渲染, 所以使用计算属性处理两个数据监听两个数据变化, 再传入scroll中
      return this.hotKey.concat(this.searchHistory)
    }
    // ...mapGetters([
    //   'searchHistory'
    // ])
  },
  created() {
    this._getHotKey()
  },
  methods: {
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''

      this.$refs.shortcutWrapper.style.bottom = bottom
      this.$refs.shortcut.refresh()
      this.$refs.searchResult.style.bottom = bottom
      this.$refs.suggest.refresh()
    },
    // addQuery(query) {
    //   this.$refs.searchBox.setQuery(query)
    // },
    // onQueryChange(query) {
    //   // 这里我们监听 query发生变化search-box组件会派发一个query事件
    //   this.query = query
    // },
    // blurInput() {
    //   this.$refs.searchBox.blur()
    // },
    // saveSearch() {
    //   this.saveSearchHistory(this.query)
    // },
    deleteOne(item) { // 删除一个搜索历史记录
      // 这个deleteOne可以不用写, 可以直接在template中写入action的方法, 参数在SearchList基础组件中传了
      this.deleteSearchHistory(item)
    },
    deleteAll() {
      // 这个deleteAll可以不用写, 可以直接在template中写入action的方法, 参数在SearchList基础组件中传了
      this.clearSearchHistory()
    },
    showConfirm() {
      this.$refs.confirm.show()
    },
    _getHotKey() {
      getHotKey().then(res => {
        if (res.statusText === 'OK') {
          this.hotKey = res.data.result.hots
        }
      }).catch(err => {
        console.log(err)
      })
    },
    ...mapActions([
      // 'saveSearchHistory',
      // 'deleteSearchHistory',
      'clearSearchHistory'
    ])
  },
  watch: {
    query(newQuery) {
      // 因为当我们在搜索时,dom在搜索列表的vue,而不是在搜索历史, 所以当原来搜索历史不能滚动, 到从搜索栏中再添加一首就可以滚动时,
      // 添加数据的时候, 搜索历史的dom没有显示, 所以没有重新刷新成功?  判断query从有到无的变化
      if (!newQuery) {
        setTimeout(() => {
          this.$refs.shortcut.refresh()
        }, 20)
      }
    }
  },
  components: {
    SearchBox,
    Suggest,
    SearchList,
    Confirm,
    Scroll
  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/css/variable.scss";
@import "../../assets/css/mixin.scss";

.search{
  .search-box-wrapper{
    margin: 20px;
  }
  .shortcut-wrapper{
    position: fixed;
    top: 178px;
    bottom: 0;
    width: 100%;
    .shortcut{
      height: 100%;
      overflow: hidden;
      .hot-key{
        margin: 0 20px 20px 20px;
        .title{
          margin-bottom: 20px;
          font-size: $font-size-medium;
          color: $color-text-l;
        }
        .item{
          display: inline-block;
          padding: 5px 10px;
          margin: 0 20px 10px 0;
          border-radius: 6px;
          background: $color-highlight-background;
          font-size: $font-size-medium;
          color: $color-text-d;
        }
      }
      .search-history{
        position: relative;
        margin: 0 20px;
        .title{
          display: flex;
          align-items: center;
          height: 40px;
          font-size: $font-size-medium;
          color: $color-text-l;
        }
        .text{
          flex: 1;
        }
        .clear{
          .icon-clear{
            font-size: $font-size-medium-x;
            color: $color-text-d;
          }
        }
      }
    }
  }
  .search-result{
    position: fixed;
    width: 100%;
    top: 178px;
    bottom: 0;
  }
}
</style>